class_name Raindrop
extends Node2D


@onready var rain: Sprite2D = $Rain
@onready var on_floor: Sprite2D = $OnFloor
@onready var timer: Timer = $Timer

# var _range_rect: Rect2
var _original_pos: Vector2
var _direction: Vector2
var _speed: float
var _distance: float
var _falling: bool


func _ready() -> void:
    timer.timeout.connect(_on_timer_timeout)

    rain.hide()
    on_floor.hide()


func _process(delta: float) -> void:
    if not _falling: return

    global_position += _direction * _speed * delta

    if global_position.distance_squared_to(_original_pos) > _distance ** 2:
        _falling = false
        rain.hide()
        on_floor.show()
        timer.start()


func setup(pos: Vector2, direction: Vector2, speed: float, distance: float) -> void:
    global_position = pos
    _original_pos = pos
    _direction = direction
    _speed = speed
    _distance = distance

    rain.frame = randi_range(0, rain.hframes - 1)
    on_floor.frame = randi_range(0, on_floor.hframes - 1)

    rain.show()
    on_floor.hide()
    _falling = true


func _on_timer_timeout() -> void:
    on_floor.hide()
    ObjectPool.release_obj(self)